1
|
|
|
/* |
2
|
|
|
* Copyright 2011 Johannes M. Schmitt <[email protected]> |
3
|
|
|
* |
4
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
5
|
|
|
* you may not use this file except in compliance with the License. |
6
|
|
|
* You may obtain a copy of the License at |
7
|
|
|
* |
8
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
9
|
|
|
* |
10
|
|
|
* Unless required by applicable law or agreed to in writing, software |
11
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
12
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13
|
|
|
* See the License for the specific language governing permissions and |
14
|
|
|
* limitations under the License. |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @fileoverview Contains some utility functions |
19
|
|
|
* |
20
|
|
|
* The implementation of these methods are taken from the Google Closure Library, |
21
|
|
|
* but may be overridden by using implementations of jQuery et. al. |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
goog.provide('twig'); |
|
|
|
|
25
|
|
|
goog.provide('twig.StringBuffer'); |
26
|
|
|
|
27
|
|
|
goog.require('goog.string'); |
28
|
|
|
goog.require('goog.string.StringBuffer'); |
29
|
|
|
goog.require('goog.object'); |
30
|
|
|
goog.require('goog.array'); |
31
|
|
|
|
32
|
|
|
twig.inherits = goog.inherits; |
|
|
|
|
33
|
|
|
twig.bind = goog.bind; |
34
|
|
|
|
35
|
|
|
goog.UID_PROPERTY_ = 'twig_ui_' + |
36
|
|
|
Math.floor(Math.random() * 2147483648).toString(36); |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @constructor |
40
|
|
|
* @extends {goog.string.StringBuffer} |
41
|
|
|
*/ |
42
|
|
|
twig.StringBuffer = goog.string.StringBuffer; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Whether the given value is considered empty. |
46
|
|
|
* |
47
|
|
|
* @param {*} value |
48
|
|
|
* @return {boolean} |
49
|
|
|
*/ |
50
|
|
|
twig.empty = function(value) { |
51
|
|
|
if (null === value || false === value || undefined === value |
52
|
|
|
|| 0 === value) { |
53
|
|
|
return true; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (twig.countable(value)) { |
|
|
|
|
57
|
|
|
return 0 === twig.count(value); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return false; |
61
|
|
|
}; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param {Object} target |
65
|
|
|
* @param {...Object} var_args |
66
|
|
|
* @return {Object} The target object |
67
|
|
|
*/ |
68
|
|
|
twig.extend = function(target, var_args) { |
|
|
|
|
69
|
|
|
goog.object.extend.apply(null, Array.prototype.slice.call(arguments, 0)); |
|
|
|
|
70
|
|
|
|
71
|
|
|
return target; |
72
|
|
|
}; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @enum {string} |
76
|
|
|
*/ |
77
|
|
|
twig.AttrAccess = { |
|
|
|
|
78
|
|
|
ANY: 'any', |
79
|
|
|
ARRAY: 'array', |
80
|
|
|
METHOD: 'method' |
81
|
|
|
}; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Returns the value of the given attr for the given object. |
85
|
|
|
* |
86
|
|
|
* @param {Object|Array} obj |
87
|
|
|
* @param {string|number} attr |
88
|
|
|
* @param {Array.<*>=} opt_args |
89
|
|
|
* @param {twig.AttrAccess=} opt_accessType |
90
|
|
|
* @param {boolean=} opt_isTest |
91
|
|
|
* @return {*} |
92
|
|
|
*/ |
93
|
|
|
twig.attr = function(obj, attr, opt_args, opt_accessType, opt_isTest) { |
94
|
|
|
var accessType = opt_accessType || twig.AttrAccess.ANY; |
|
|
|
|
95
|
|
|
var isTest = goog.isDef(opt_isTest) ? opt_isTest : false; |
|
|
|
|
96
|
|
|
|
97
|
|
|
if (!goog.isObject(obj) && !goog.isArray(obj)) { |
98
|
|
|
return isTest ? false : null; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (attr in obj) { |
102
|
|
|
if (twig.AttrAccess.ARRAY !== accessType |
103
|
|
|
&& goog.isFunction(obj[attr])) { |
104
|
|
|
if (isTest) { |
105
|
|
|
return true; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return obj[attr].apply(obj, opt_args || []); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (twig.AttrAccess.METHOD !== accessType) { |
112
|
|
|
if (isTest) { |
113
|
|
|
return true; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return obj[attr]; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if (twig.AttrAccess.ARRAY === accessType |
121
|
|
|
|| goog.isArray(obj)) { |
122
|
|
|
if (isTest) { |
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// FIXME: Should we add strict behavior similar to Twig's implementation? |
127
|
|
|
return null; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// check for getters/issers |
131
|
|
|
attr = attr.toLowerCase(); |
132
|
|
|
var getter = 'get' + attr; |
133
|
|
|
var isser = 'is' + attr; |
134
|
|
|
var functionName = goog.object.findKey(obj, function(v, k) { |
135
|
|
|
k = k.toLowerCase(); |
136
|
|
|
|
137
|
|
|
return k === getter || k === isser; |
138
|
|
|
}); |
139
|
|
|
|
140
|
|
|
if (functionName && goog.isFunction(obj[functionName])) { |
|
|
|
|
141
|
|
|
if (isTest) { |
142
|
|
|
return true; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return obj[functionName].apply(obj, opt_args || []); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if (isTest) { |
149
|
|
|
return false; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
// FIXME: Strict behavior? |
153
|
|
|
return null; |
154
|
|
|
}; |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Removes all non-meaningful spaces from the HTML. |
158
|
|
|
* |
159
|
|
|
* @param {string} s |
160
|
|
|
* @return {string} |
161
|
|
|
*/ |
162
|
|
|
twig.spaceless = function(s) { |
|
|
|
|
163
|
|
|
// Since IE doesn't include non-breaking-space (0xa0) in their \s character |
164
|
|
|
// class (as required by section 7.2 of the ECMAScript spec), we explicitly |
165
|
|
|
// include it in the regexp to enforce consistent cross-browser behavior. |
166
|
|
|
return goog.string.trim(s.replace(/>[\s\xa0]+</g, "><")); |
|
|
|
|
167
|
|
|
}; |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Port of the PHP range() function. |
171
|
|
|
* |
172
|
|
|
* @param {number} start |
173
|
|
|
* @param {number} end |
174
|
|
|
* @return {Array.<number>} |
175
|
|
|
*/ |
176
|
|
|
twig.range = function(start, end) { |
|
|
|
|
177
|
|
|
var rs = []; |
178
|
|
|
for (;start <= end; start += 1) { |
179
|
|
|
rs.push(start); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return rs; |
183
|
|
|
}; |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param {Array|Object|string} haystack |
187
|
|
|
* @param {*} needle |
188
|
|
|
* @return {boolean} |
189
|
|
|
*/ |
190
|
|
|
twig.contains = function(haystack, needle) { |
|
|
|
|
191
|
|
|
if (goog.isArray(haystack)) { |
|
|
|
|
192
|
|
|
return goog.array.contains(/** @type {Array} */ (haystack), needle); |
193
|
|
|
} |
194
|
|
|
if (goog.isString(haystack)) { |
195
|
|
|
return ( |
196
|
|
|
goog.string.contains(haystack, /** @type {string} */ (needle)) |
197
|
|
|
&& (needle !== "" || haystack === "") |
198
|
|
|
); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return goog.object.contains(/** @type {Object} */ (haystack), needle); |
202
|
|
|
}; |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Returns whether the given value is countable. |
206
|
|
|
* |
207
|
|
|
* @param {*} v |
208
|
|
|
* @return {boolean} |
209
|
|
|
*/ |
210
|
|
|
twig.countable = function(v) { |
|
|
|
|
211
|
|
|
return goog.isArray(v) || goog.isString(v) || goog.isObject(v); |
|
|
|
|
212
|
|
|
}; |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Returns the count for the given value. |
216
|
|
|
* |
217
|
|
|
* @param {*} v |
218
|
|
|
* @return {number} |
219
|
|
|
*/ |
220
|
|
|
twig.count = function(v) { |
|
|
|
|
221
|
|
|
if (goog.isArray(v)) { |
|
|
|
|
222
|
|
|
return v.length; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
if (goog.isString(v)) { |
226
|
|
|
return v.length; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
if (goog.isObject(v)) { |
230
|
|
|
return goog.object.getCount(v); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return twig.castToString(v).length; |
|
|
|
|
234
|
|
|
}; |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Returns the given value as a string |
238
|
|
|
* |
239
|
|
|
* @param {*} value |
240
|
|
|
* @return {string} |
241
|
|
|
*/ |
242
|
|
|
twig.castToString = function(value) { |
243
|
|
|
if (typeof value === "number") { |
244
|
|
|
return value.toString(); |
245
|
|
|
} |
246
|
|
|
return ""; |
247
|
|
|
}; |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* |
251
|
|
|
* @param {Array|Object} v |
252
|
|
|
* @param {Function} func |
253
|
|
|
* @param {Object=} opt_this |
254
|
|
|
*/ |
255
|
|
|
twig.forEach = function(v, func, opt_this) { |
|
|
|
|
256
|
|
|
if (goog.isArray(v)) { |
|
|
|
|
257
|
|
|
goog.array.forEach(/** @type {Array} */ (v), func, opt_this); |
258
|
|
|
|
259
|
|
|
return; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
goog.object.forEach(/** @type {Object} */ (v), func, opt_this); |
263
|
|
|
}; |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Creates an object literal if keys can be dynamic. |
267
|
|
|
* |
268
|
|
|
* @param {...*} var_args |
269
|
|
|
* @return {Object} |
270
|
|
|
*/ |
271
|
|
|
twig.createObj = function(var_args) { |
|
|
|
|
272
|
|
|
var rs = {}; |
273
|
|
|
for (var i = 0; i<arguments.length; i += 2) { |
274
|
|
|
rs[arguments[i]] = arguments[i+1]; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
return rs; |
278
|
|
|
}; |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Escapes any special characters in a string for use in a regular expression |
282
|
|
|
* |
283
|
|
|
* @param {string} string |
284
|
|
|
* @return {string} |
285
|
|
|
*/ |
286
|
|
|
twig.pregQuote = function(string) { |
|
|
|
|
287
|
|
|
return string.replace(/[\.\\+*?\[\]<>(){}^$=!|:-]/g, '\\$&'); |
288
|
|
|
}; |
289
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.